{
  Nassy - anti-spam for IM clients
  Copyright (C) 2008-2009 Denis Shemanayev aka nis
                          mailto: nis@nis.org.ua
                          mailto: Denis.Shemanayev@gmail.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
}
{ $Id: uLogForm.pas 2 2009-06-07 14:43:34Z Denis.Shemanayev $ }

unit uLogForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TntForms, StdCtrls, Buttons, TntButtons, ExtCtrls, TntExtCtrls,
  RVScroll, RichView, RVStyle, Menus, TntMenus, uMainForm, ImgList, uConst;

type
  TfrmLog = class(TfrmMain)
    PnlBottom: TTntPanel;
    BtnOk: TTntBitBtn;
    BtnOpenFolder: TTntBitBtn;
    BtnClear: TTntBitBtn;
    PnlCount: TTntPanel;
    RVLog: TRichView;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    RVStyle1: TRVStyle;
    MnuLog: TTntPopupMenu;
    MnuLogCopy: TTntMenuItem;
    MnuLogCopyall: TTntMenuItem;
    procedure TntFormCreate(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure BtnClearClick(Sender: TObject);
    procedure TntFormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnOpenFolderClick(Sender: TObject);
    procedure TntFormResize(Sender: TObject);
    procedure MnuLogCopyClick(Sender: TObject);
    procedure MnuLogCopyallClick(Sender: TObject);
  private
  public
    procedure Add(Sender, Text: WideString; Time: LongInt; Probability: Extended);
  end;

var
  frmLog: TfrmLog;

implementation

uses
  uQipPlugin, DateUtils, TntWindows, uInfMenu,
  gnugettext, TntSysUtils, uOptionsForm;

{$R *.DFM}

{***********************************************************}
procedure TfrmLog.TntFormCreate(Sender: TObject);
var
  i: Integer;
begin
  frmLog := self;
  PnlCount.Caption := '0';
  Plugin.ILButtons.GetIcon(8, Icon);
  Plugin.ILButtons.GetBitmap(4, BtnClear.Glyph);
  Plugin.ILButtons.GetBitmap(2, BtnOpenFolder.Glyph);
  Plugin.ILButtons.GetBitmap(0, BtnOk.Glyph);
  MnuLog.Images := Plugin.ILMenus;
  InfMenu.Add(MnuLog);
  i := 0;
  while i <= Plugin.Log.Count - 1 do
  begin
    if RVLog.LineCount > 0 then
      RVLog.AddBreak;
    RVLog.AddFromNewLine(Plugin.Log.Strings[i], rvsHeading);
    RVLog.AddTextFromNewLine(Tnt_WideStringReplace(Plugin.Log.Strings[i + 1], '\n',
     CRLF, [rfReplaceAll]), rvsNormal);
    Inc(i ,2);
  end;
  RVLog.FormatTail;
  RVLog.Repaint;
  PnlCount.Caption := IntToStr(Plugin.Log.Count div 2);
  Plugin.ResetCounter;
end;

{***********************************************************}
procedure TfrmLog.BtnOkClick(Sender: TObject);
begin
  Close;
end;

{***********************************************************}
procedure TfrmLog.BtnClearClick(Sender: TObject);
begin
  Plugin.Log.Clear;
  PnlCount.Caption := '0';
  RVLog.Clear;
  RVLog.Repaint;
end;

{***********************************************************}
procedure TfrmLog.Add(Sender, Text: WideString; Time: Integer; Probability: Extended);
begin
  if RVLog.LineCount > 0 then
    RVLog.AddBreak;
  RVLog.AddFromNewLine(Sender, rvsHeading);
  RVLog.Add(FormatDateTime(C_LOG_DATE_FMT, UnixToDateTime(Time)), rvsHeading);
  RVLog.AddTextFromNewLine(Text, rvsNormal);
  RVLog.FormatTail;
  RVLog.Repaint;
  PnlCount.Caption := IntToStr(StrToInt(PnlCount.Caption) + 1); // craps!
end;

{***********************************************************}
procedure TfrmLog.TntFormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  frmLog := nil;
  Plugin.ResetCounter;
end;

{***********************************************************}
procedure TfrmLog.BtnOpenFolderClick(Sender: TObject);
begin
  Tnt_ShellExecuteW(0, 'explore', PWideChar(Plugin.DirData + C_DIR_LOGS),
    nil, nil, SW_SHOW);
end;

{***********************************************************}
procedure TfrmLog.TntFormResize(Sender: TObject);
begin
  PnlCount.Left := Width - PnlCount.Width - 18;
end;

{***********************************************************}
procedure TfrmLog.MnuLogCopyClick(Sender: TObject);
begin
  RVLog.CopyText;
end;

{***********************************************************}
procedure TfrmLog.MnuLogCopyallClick(Sender: TObject);
begin
  if RVLog.lines.Count = 0 then
    Exit;
  RVLog.SelectAll;
  RVLog.CopyText;
  RVLog.Deselect;
end;

end.
